<?php
/*** 
This file gets files to be processed as POST -parameters and concates the
filepaths of the files the user wants to anonymize. The files are copied to
the processed folder and appended an '.html' to the end of the filename.

The files, input output and filterfile are then given as a parameter to the 
java logic, which is also run in the file through shell.
***/

session_start();
include_once("./configuration.php");
if($_SESSION['logged']) {
	$filefolder = $_SESSION['uploads'];
	$processfolder = $_SESSION['processed'];
	$filterlist = $_SESSION['filterlist'].$_SESSION['filterlist_file'];
	$javadir = dirname(__FILE__)."/logic/";
	
	$first_file = "";
	$inputFiles = "";
	$outputFiles = "";
	//TODO: Make it start from number 1. Needs refactoring in some other file... Ask Ats Nisov.
	$i = 2;
	while (isset($_POST["arvo$i"])) {
	  if (isset($_POST["testiboksi$i"]) && $_POST["testiboksi$i"] == "on") {
	    if ($first_file == "") {
	      $first_file = $_POST["arvo$i"];
	    }
	  
	    $inputFiles .= $filefolder.$_POST["arvo$i"]." "; 

	    $outputFiles .= $processfolder.$_POST["arvo$i"].".html ";
	    shell_exec("cp ".$filefolder.$_POST["arvo$i"]." ".$processfolder.$_POST["arvo$i"]);
	    @unlink($_SESSION['homedir'].$_POST["arvo$i"].".filter.txt");
	    @unlink($_SESSION['homedir'].$_POST["arvo$i"].".anonymize.txt");
	    @unlink($_SESSION['completed'].$_POST["arvo$i"]."*");
	  }
	  
	  $i++;
	}
	trim($inputFiles);
	trim($outputFiles);
	
	$cmd = "cd $javadir && export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.21 ; java -jar logic.jar -level ".escapeshellarg($_POST['level'])." -input ".$inputFiles." -output ".$outputFiles." -ignore ".$filterlist;
	shell_exec($cmd);
}
header("Location: ./index.php?p=edit&file=$first_file");
?>
